<?php
/**
 * User: zwh
 * Time: 2016-10-24 15:56:56
 */

class MirrorOrderProduct extends Eloquent
{
    protected $table = "mirror_order_product";
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 10:00:32
     * @Description: 用于前端获取镜像订单 产品信息的 方法
     */
    public static function  getOrderProductById($order_id){
        $order=MirrorOrders::find($order_id);
        if($order->engineer_id==0){
            $engineer_name="无";
        }else{
            $me=MaintenanceEngineers::find($order->engineer_id);
            $engineer_name=$me->name;
            $engineers[$order->engineer_id]=$engineer_name;
        }
        $order_product=MirrorOrderProduct::where("order_id",$order_id)->where("status",1)->orderBy('product_type','desc')->get();
        $activity_product=$enterprise_product=$promotion_procut=$base_procut=$non_standard=$remind=array();
        foreach ($order_product as $_op) {
            switch ($_op->product_type) {
                case 'enterprise':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    $product_row=0;
                    $opep=MirrorOrderProductEsPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opep as $key => $value){
                        $temp_product=ProductPackage::find($value->package_id);
                        $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                        if($scf){//说明配置了该服务分类的分类提醒
                            if(!isset($remind[$scf->sc_id])){
                                $remind[$scf->sc_id]=$scf;
                            }
                        }
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }
                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            }else{
                                $_item->unit='个';
                            }
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                    }
                    $engineers=array_values($engineers);
                    $_op->package=$opep;
                    $_op->col=$product_row;
                    $_op->engineers=$engineers;
                    $enterprise_product[]=$_op;
                    break;
                case 'activity':
                    $engineers=array();
                    $engineers[$order->engineer_id]=$engineer_name;
                    $product_row=0;
                    $opacp=MirrorOrderProductAcPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opacp as $key => $value){
                        $temp_product=ProductPackage::find($value->package_id);
                        $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                        if($scf){//说明配置了该服务分类的分类提醒
                            if(!isset($remind[$scf->sc_id])){
                                $remind[$scf->sc_id]=$scf;
                            }
                        }
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                // if($_item->item_type==0){
                                    if($_item->is_adapter==1){
                                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                        $_item->scf_name=$item_info->scf_name;
                                        $_item->subject=$item_info->subject;
                                        $_item->subject_name=$item_info->subject_name;
                                        $_item->brand_name=$item_info->brand_name;
                                        $_item->specification=$item_info->specification;
                                        $_item->series_name=$item_info->series_name;
                                    }else{
                                        $_item->scf_name="";
                                        $_item->subject=$_item->item_id;
                                        $_item->subject_name="";
                                        $_item->brand_name="";
                                        $_item->specification="";
                                    }
                                // }else{
                                //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                //     $_item->scf_name=$item_info->scf_name;
                                //     $_item->subject=$item_info->subject;
                                //     $_item->subject_name=$item_info->subject_name;
                                //     $_item->brand_name=$item_info->brand_name;
                                //     $_item->specification=$item_info->specification;
                                //     $_item->series_name=$item_info->series_name;
                                // }
                                $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                            }else{
                                $_item->unit='个';
                            }
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                            $engineers[$order->engineer_id]=$mes->name;
                        }
                    }
                    $engineers=array_values($engineers);
                    $_op->package=$opacp;
                    $_op->col=$product_row;
                    $_op->engineers=$engineers;
                    $activity_product[]=$_op;
                    break;
                case 'promotion':
                    $product_row=0;
                    $opp=MirrorOrderProductPackage::with("item")->where("order_product_id",$_op->id)->get();
                    foreach ($opp as $key => $value){
                        $value->col=sizeof($value->item);
                        $product_row=$product_row+sizeof($value->item);
                        foreach ($value->item as $_item) {
                            if($_item->type==1){
                                $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                $_item->scf_name=$item_info->scf_name;
                                $_item->unit=$item_info->unit;
                                $_item->unit=$item_info->subject;
                                $_item->subject_name=$item_info->subject_name;
                                $_item->brand_name=$item_info->brand_name;
                                $_item->specification=$item_info->specification;
                            }else{
                                $_item->unit='个';
                            }
                        }
                        if($value->engineer_id==0){
                            $value->engineer_name=$engineer_name;
                        }else{
                            $mes=MaintenanceEngineers::find($value->engineer_id);
                            $value->engineer_name=$mes->name;
                        }
                    }
                    $_op->package=$opp;
                    $_op->col=$product_row;
                    $promotion_procut[]=$_op;
                    break;
                case 'base':
                    $temp_product=ProductPackage::find($_op->product_id);
                    $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                    if($scf){//说明配置了该服务分类的分类提醒
                        if(!isset($remind[$scf->sc_id])){
                            $remind[$scf->sc_id]=$scf;
                        }
                    }
                    $product_row=0;
                    $opp=MirrorOrderProductBaseItem::where("order_product_package_id",$_op->id)->get();
                    foreach ($opp as $key=>$_item) {
                        if($_item->type==1){
                            // if($_item->item_type==0){
                                if($_item->is_adapter==1){
                                    $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                    $_item->scf_name=$item_info->scf_name;
                                    $_item->subject=$item_info->subject;
                                    $_item->subject_name=$item_info->subject_name;
                                    $_item->brand_name=$item_info->brand_name;
                                    $_item->specification=$item_info->specification;
                                    $_item->series_name=$item_info->series_name;
                                }else{
                                    $_item->scf_name="";
                                    $_item->subject=$_item->item_id;
                                    $_item->subject_name="";
                                    $_item->brand_name="";
                                    $_item->specification="";
                                }
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                            //     $_item->scf_name=$item_info->scf_name;
                            //     $_item->subject=$item_info->subject;
                            //     $_item->subject_name=$item_info->subject_name;
                            //     $_item->brand_name=$item_info->brand_name;
                            //     $_item->specification=$item_info->specification;
                            //     $_item->series_name=$item_info->series_name;
                            // }
                            $_item->temp_subject=($_item->temp_subject==0)?$_item->subject:$_item->temp_subject;
                        }else{
                            $_item->unit='个';
                        }
                    }
                    if($_op->engineer_id==0){
                        $_op->engineer_name=$engineer_name;
                    }else{
                        $mes=MaintenanceEngineers::find($_op->engineer_id);
                        $_op->engineer_name=$mes->name;
                    }
                    $_op->item=$opp;
                    $_op->col=sizeof($opp);
                    $base_procut[]=$_op;
                    break;
                case 'non_standard':
                    $temp_product=Service::find($_op->product_id);
                    $scf=ServiceClassificationRemind::where("sc_id",$temp_product->classify)->where("status",1)->select("sc_id","content")->first();
                    if($scf){//说明配置了该服务分类的分类提醒
                        if(!isset($remind[$scf->sc_id])){
                            $remind[$scf->sc_id]=$scf;
                        }
                    }
                    $opn=MirrorOrderProductNonStandard::where("order_product_id",$_op->id)->where('status',1)->orderBy('service_type','asc')->get();
                    foreach ($opn as $key=>$value) {
                        //判断产品项是非标还是标准配件
                        if($value->service_type==1){
                            // if($value->type==0){
                                $item_info=SupplyItem::getCacheItemInfo($value->item_id);
                                $value->scf_name=$item_info->scf_name;
                                $value->subject=$item_info->subject;
                                $value->subject_name=$item_info->subject_name;
                                $value->brand_name=$item_info->brand_name;
                                $value->specification=$item_info->specification;
                                $value->series_name=$item_info->series_name;
                            // }else{
                            //     $item_info=SupplyItemNonStandard::getCacheItemInfo($value->item_id);
                            //     $value->scf_name=$item_info->scf_name;
                            //     $value->subject=$item_info->subject;
                            //     $value->subject_name=$item_info->subject_name;
                            //     $value->brand_name=$item_info->brand_name;
                            //     $value->specification=$item_info->specification;
                            //     $value->series_name=$item_info->series_name;
                            // }
                            $value->temp_subject=$value->subject;
                        }else{
                            $value->unit='个';
                        }
                    }
                    if($_op->engineer_id==0){
                        $_op->engineer_name=$engineer_name;
                    }else{
                        $mes=MaintenanceEngineers::find($_op->engineer_id);
                        $_op->engineer_name=$mes->name;
                    }
                    $_op->item=$opn;
                    $_op->col=sizeof($opn);
                    $non_standard[]=$_op;
                    break;
            }
        }
        ksort($remind);
        return array("activity_product"=>$activity_product,"enterprise_product"=>$enterprise_product,"promotion_procut"=>$promotion_procut,"base_procut"=>$base_procut,"non_standard"=>$non_standard,"remind"=>array_values($remind));
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:11:10
     * @Description: 添加 基础产品的 方法
     */
    public static function saveBaseProducts($package_arr,$type,$sc_user_id,$order_id){
        return DB::transaction(function () use ($package_arr,$type,$sc_user_id,$order_id) {
            $order=MirrorOrders::where("order_id",$order_id)->lockForUpdate()->first();
            $temp_amount=0;
            foreach ($package_arr as $_pa) {
                $order_product=new MirrorOrderProduct;
                $order_product->order_id=$order_id;
                $order_product->product_type="base";
                $order_product->product_id=$_pa["product_id"];
                $order_product->product_name=$_pa["product_name"];
                $order_product->status=1;
                $order_product->amount=$_pa["amount"];
                $order_product->save();
                //添加各个产品项
                foreach ($_pa["item"] as $_pi) {
                    $opi=new MirrorOrderProductBaseItem;
                    $opi->order_id=$order_id;
                    $opi->order_product_package_id=$order_product->id;
                    $opi->item_id=$_pi["item_id"];
                    $opi->item_name=$_pi["item_name"];
                    $opi->amount=$_pi["amount"];
                    $opi->num=$_pi["num"];
                    $opi->type=$_pi["type"];
                    $opi->unit=$_pi["unit"];
                    $opi->is_adapter=$_pi["is_adapter"];
                    $opi->save();
                }
            }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate($order_id);
            return $order_id;
        });
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-25 15:31:19
     * @Description: 用于删除 一个完整的产品的方法 
     */
    public static function deleteProduct($order_product){
        return DB::transaction(function () use ($order_product) {
            $order_product->status=0;
            $order_product->save();
    
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate($order_product->order_id);
            return array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_product->order_id));
        });
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-26 10:29:54
     * @Description: 虚拟单添加企业包
     */
    public static function implement($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi){
        return DB::transaction(function () use ($pids,$sc_user,$orders,$wmcf,$whp,$es,$csi) {
    
            $order_product=new MirrorOrderProduct;
            $order_product->order_id=$orders->order_id;
            $order_product->product_type="enterprise";
            $order_product->product_id=$es->id;
            $order_product->product_name=$es->name;
            $order_product->status=1;
            $order_product->amount=$es->amount;
            $order_product->save();
            $order=MirrorOrders::where("order_id",$orders->order_id)->lockForUpdate()->first();
            foreach($pids as $pi){
                $package=ProductPackage::find($pi);
                if(is_null($package)){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_error_50001");
                    return $response;
                }
                if($package->status!=1){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_status_error_50002");
                    return $response;
                }
                if($package->place!=2){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_unable_error_50003");
                    return $response;
                }
                $p_item=ProductPackage::getItem($orders->car_model_id,$package->id);
                $count_amount=0;
                $supply_arr=$p_item['supply'];
                $order_product_item=array();
                foreach ($supply_arr as $_sa) {
                    //更换耗材的加价系数
                    // $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    // if($wpc){
                    //     $wmcf->supply_coefficient=$wpc->coefficient;
                    // }else{
                    //     $wmcf->supply_coefficient=1;
                    // }
                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    $supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($supply_coefficient == null){ //如果是null，说明没有赋值
                        $supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }
                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $_sa["amount"]=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                    if($sip){
                        $_sa["amount"]=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                        $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
    
                    $temp_amount=$_sa["amount"]*$supply_coefficient;
                    $temp_amount=Common::upFow($temp_amount);
                    $order_product_item[]=array(
                        "item_id"=>$_sa["id"],
                        "item_name"=>$_sa["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_sa["number"],
                        "type"=>1,
                        "unit"=>$_sa["unit"],
                        "is_adapter"=>$_sa["is_adapter"]
                    );
                    $count_amount+=$temp_amount*$_sa["number"];
                }
                foreach ($p_item['server'] as $_se) {
                    $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                    if($res['code']==0){
                        $temp_amount=$res['price'];
                    }else{
                        //判断工时类型为工时类型还是固定价格
                        if($_se['type'] ==1 ){
                            $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                        }elseif($_se['type']==2){
                            $temp_amount=$_se["price"];
                        }
                    }
                    $order_product_item[]=array(
                        "item_id"=>$_se["id"],
                        "item_name"=>$_se["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_se["work_hours"],
                        "type"=>2,
                        "unit"=>"个",
                        "is_adapter"=>1
                    );
                    $count_amount+=$temp_amount;
                }
    
    
                $opp=new MirrorOrderProductEsPackage;
                $opp->order_id=$orders->order_id;
                $opp->order_product_id=$order_product->id;
                $opp->package_id=$pi;
                $opp->package_name=$package->name;
                $opp->work_station=0;
                $opp->work_status='prepare';
                $opp->amount=$count_amount;
                $opp->engineer_id=0;
                $opp->save();
    
    
    
                //添加各个产品项
                foreach ($order_product_item as $opi) {
                    $opesi=new MirrorOrderProductEsItem;
                    $opesi->order_id=$orders->order_id;
                    $opesi->order_product_package_id=$opp->id;
                    $opesi->item_id=$opi["item_id"];
                    $opesi->item_name=$opi["item_name"];
                    $opesi->amount=$opi["amount"];
                    $opesi->num=$opi["num"];
                    $opesi->type=$opi["type"];
                    $opesi->item_type=0;
                    $opesi->unit=$opi["unit"];
                    $opesi->is_adapter=$opi['is_adapter'];
                    $opesi->save();
                }
            }
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate($orders->order_id);
            return $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        });
    }
    
    
    /**
     * @Author:      zwh
     * @DateTime:    2017-4-11 14:11:16
     * @Description: 虚拟单添加活动包
     */
    public static function addProductActivity($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi){
        return DB::transaction(function () use ($pids,$sc_user,$orders,$wmcf,$whp,$pa,$csi) {
            $order_product=new MirrorOrderProduct;
            $order_product->order_id=$orders->order_id;
            $order_product->product_type="activity";
            $order_product->product_id=$pa->id;
            $order_product->product_name=$pa->name;
            $order_product->status=1;
            if($pa->type==1){
                $order_product->amount=$pa->price;
            }else{
                $order_product->amount=0;
            }
            $order_product->save();
            $order=MirrorOrders::where("order_id",$orders->order_id)->lockForUpdate()->first();
            $total=0;
            foreach($pids as $pi){
                $count_amount=0;
                $package=ProductPackage::find($pi);
                if(is_null($package)){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_error_50001");
                    return $response;
                }
                if($package->status!=1){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_status_error_50002");
                    return $response;
                }
                if($package->place!=2){
                    DB::rollback();
                    $response = Lang::get("scs_error.package_unable_error_50003");
                    return $response;
                }
                $p_item=ProductPackage::getItem($orders->car_model_id,$package->id);
                $supply_arr=$p_item['supply'];
                $order_product_item=array();
                foreach ($supply_arr as $_sa) {
                    //更换耗材的加价系数
                    // $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    // if($wpc){
                    //     $wmcf->supply_coefficient=$wpc->coefficient;
                    // }else{
                    //     $wmcf->supply_coefficient=1;
                    // }
                    $wpc=WorkPartsCoefficient::where("car_model_name",$csi->model_level)->where("supply_brand_id",$_sa["brand_id"])->where("supply_subject_id",$_sa["subject"])->where("status",1)->first();
                    $wmcf->supply_coefficient = null;
                    if (!empty($wpc)) {
                        $wpc_city=WorkPartsCoefficientCity::where('city_id',$sc_user->shop->city_id)->where('coefficient_id',$wpc->id)->where('status',1)->first();
                        $wmcf->supply_coefficient=isset($wpc_city->coefficient) ? $wpc_city->coefficient : null; //如果有城市系数，优先使用。
                    }
                    if ($wmcf->supply_coefficient == null){ //如果是0，说明没有赋值
                        $wmcf->supply_coefficient=isset($wpc->coefficient) ? $wpc->coefficient : 1;
                    }
                    //更换多城市价格
                    // $sip=SupplyItemPrice::where("item_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->first();
                    // if($sip){
                    //     $_sa["amount"]=$sip->price;
                    // }
                    $sip=SupplyCityPrice::where("supply_id",$_sa["id"])->where("city_id",$sc_user->shop->city_id)->where("status",1)->first();
                    if($sip){
                        $_sa["amount"]=$sip->price;
                    }else{
                        $ss_temp=SupplySub::where("supply_id",$_sa["id"])->where("status",1)->first();
                        $_sa["amount"]=isset($ss_temp)?$ss_temp->usual_price:0;
                    }
    
                    $temp_amount=$_sa["amount"]*$wmcf->supply_coefficient;
                    $temp_amount=Common::upFow($temp_amount);
                    $order_product_item[]=array(
                        "item_id"=>$_sa["id"],
                        "item_name"=>$_sa["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_sa["number"],
                        "type"=>1,
                        "unit"=>$_sa["unit"],
                        "is_adapter"=>$_sa["is_adapter"]
                    );
                    $count_amount+=$temp_amount*$_sa["number"];
                    $total+=$temp_amount*$_sa["number"];
                }
                foreach ($p_item['server'] as $_se) {
                    $res=PricingStrategy::getStringStrategy($csi->id,$_se['id'],$sc_user->shop->city_id);
                    if($res['code']==0){
                        $temp_amount=$res['price'];
                    }else{
                        //判断工时类型为工时类型还是固定价格
                        if($_se['type'] ==1 ){
                            $temp_amount=$whp->price*$wmcf->work_coefficient*$_se["work_hours"];
                        }elseif($_se['type']==2){
                            $temp_amount=$_se["price"];
                        }
                    }
                    $order_product_item[]=array(
                        "item_id"=>$_se["id"],
                        "item_name"=>$_se["name"],
                        "amount"=>$temp_amount,
                        "num"=>$_se["work_hours"],
                        "type"=>2,
                        "unit"=>"个",
                        "is_adapter"=>1
                    );
                    $count_amount+=$temp_amount;
                    $total+=$temp_amount;
                }
    
    
                $opp=new MirrorOrderProductAcPackage;
                $opp->order_id=$orders->order_id;
                $opp->order_product_id=$order_product->id;
                $opp->package_id=$pi;
                $opp->package_name=$package->name;
                $opp->work_station=0;
                $opp->work_status='prepare';
                $opp->amount=$count_amount;
                $opp->engineer_id=0;
                $opp->save();
    
    
    
                //添加各个产品项
                foreach ($order_product_item as $opi) {
                    $opaci=new MirrorOrderProductAcItem;
                    $opaci->order_id=$orders->order_id;
                    $opaci->order_product_package_id=$opp->id;
                    $opaci->item_id=$opi["item_id"];
                    $opaci->item_name=$opi["item_name"];
                    $opaci->amount=$opi["amount"];
                    $opaci->num=$opi["num"];
                    $opaci->type=$opi["type"];
                    $opaci->item_type=0;
                    $opaci->unit=$opi["unit"];
                    $opaci->discount=$pa->discount;
                    $opaci->is_adapter=$opi['is_adapter'];
                    $opaci->save();
                }
            }
            $order_product->save();
            //订单应付金额及优惠信息将重新计算，废弃原有再优惠信息上加减
            MirrorOrders::calculate($orders->order_id);
            return $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        });
    }
}
